<?php
/**
 * Created by PhpStorm.
 * User: 35083806
 * Date: 2019/9/17
 * Time: 15:39
 */

/**
 * 给所有设备下发通知消息
 */
function pushTengXunSingle($msg, $userArray, $plate = 1, $app_source = 0)
{

    $msg['cont'] = $msg['desc'];
    if ($plate == 2)//android
    {
        $res = pushTengXunAndroid(1, $userArray, $msg, $app_source);
    } else if ($plate == 1)//ios开发环境
    {
        $res = pushTengXunIos(1, $userArray, $msg, $app_source);
    } else if ($plate == 3)//ios产品环境
    {
        $res = pushTengXunIosProduct(1, $userArray, $msg, $app_source);
    }

    if ($res['ret_code'] == '0') {
        return array('err' => 0, 'msg' => '成功', 't' => $res);
    } else {
        return array('err' => 1, 'msg' => '失败', 't' => $res);
    }
}


/**
 * ty:1、当个设备下发通知 2、单个账号下发通知 3、给所有设备下发通知  4、给选中设备下发通知
 * token 账号 token等
 * msg:消息内容
 * @param string $ty
 */
function pushTengXunAndroid($ty = '1', $token = array(), $msg = array(), $app_source = 0)
{
    include_once(dirname(realpath(APP_PATH)) . "/vendor/xg/src/XingeApp.php");

    $config = config("txAppPushConfig.android");
    $appid = $config['appid'];
    $android_secretKey = $config['android_secretKey'];
    $accessid = $config['accessid'];
    $accesskey = $config['accesskey'];


    if ($ty == '1') {
        //给单个设备下发通知
        return (XingeApp::PushTokenAndroid($accessid, $android_secretKey,
            $msg['title'], $msg['cont'], $token[0], $msg['custom_content'], $msg['action']));
    } else if ($ty == '2') {
        //给单个帐号下发通知
        return (XingeApp::PushAccountAndroid($accessid, $android_secretKey, $msg['title'], $msg['cont'], $token[0]));
    } else if ($ty == '3') {
        //给所有设备下发通知
        return (XingeApp::PushAllAndroid($accessid, $android_secretKey, $msg['title'],
            $msg['cont'], $msg['custom_content'], $msg['action']));
    } else if ($ty == '4') {
        //给标签选中设备下发通知
        return (XingeApp::PushTagAndroid($accessid, $android_secretKey, $msg['title'], $msg['cont'], "tag"));
    }

}


/**
 * ty:1、当个设备下发通知 2、单个账号下发通知 3、给所有设备下发通知  4、给选中设备下发通知
 * @param string $ty
 */
function pushTengXunIos($ty = '1', $token = array(), $msg = array(), $app_source = 0)
{
    include_once(dirname(realpath(APP_PATH)) . "/vendor/xg/src/XingeApp.php");


    $config = config("txAppPushConfig.ios_test");
    $appid = $config['appid'];
    $ios_secretKey = $config['ios_secretKey'];
    $accessid = $config['accessid'];
    $accesskey = $config['accesskey'];

    if ($ty == '1') {
        //开发环境下 给单个设备下发通知
        return (XingeApp::PushTokenIos($accessid, $ios_secretKey, $msg['cont_ios'], $token[0], XingeApp::IOSENV_DEV, $msg['custom_content']));
    } else if ($ty == '2') {
        //开发环境下 给单个帐号下发通知
        return (XingeApp::PushAccountIos($accessid, $ios_secretKey, $msg['cont_ios'], $token[0], XingeApp::IOSENV_DEV));
    } else if ($ty == '3') {
        //开发环境下 给所有设备下发通知
        return (XingeApp::PushAllIos($accessid, $ios_secretKey, $msg['cont_ios'], XingeApp::IOSENV_DEV, $msg['custom_content']));
    } else if ($ty == '4') {
        //开发环境下 给标签选中设备下发通知
        return (XingeApp::PushTagIos($accessid, $ios_secretKey, $msg['cont_ios'], "tag", XingeApp::IOSENV_DEV));
    }
}


/**
 * ty:1、当个设备下发通知 2、单个账号下发通知 3、给所有设备下发通知  4、给选中设备下发通知
 * @param string $ty
 */
function pushTengXunIosProduct($ty = '1', $token = array(), $msg = array(), $app_source = 0)
{
    include_once(dirname(realpath(APP_PATH)) . "/vendor/xg/src/XingeApp.php");
    $config = config("txAppPushConfig.ios");
    $appid = $config['appid'];
    $ios_secretKey = $config['ios_secretKey'];
    $accessid = $config['accessid'];
    $accesskey = $config['accesskey'];
    if ($ty == '1') {
        //开发环境下 给单个设备下发通知
        return (XingeApp::PushTokenIos($accessid, $ios_secretKey, $msg['cont_ios'], $token[0], XingeApp::IOSENV_PROD, $msg['custom_content']));
    } else if ($ty == '2') {
        //开发环境下 给单个帐号下发通知
        return (XingeApp::PushAccountIos($accessid, $ios_secretKey, $msg['cont_ios'], $token[0], XingeApp::IOSENV_PROD));
    } else if ($ty == '3') {
        //开发环境下 给所有设备下发通知
        return (XingeApp::PushAllIos($accessid, $ios_secretKey, $msg['cont_ios'], XingeApp::IOSENV_PROD, $msg['custom_content']));

    } else if ($ty == '4') {
        //开发环境下 给标签选中设备下发通知
        return (XingeApp::PushTagIos($accessid, $ios_secretKey, $msg['cont_ios'], "tag", XingeApp::IOSENV_DEV));
    }
}

